/*
 * @lc app=leetcode.cn id=463 lang=csharp
 *
 * [463] 岛屿的周长
 */

// @lc code=start
public class Solution {
    public int IslandPerimeter(int[][] grid) {
        int ans = 0;
        for(int i = 0; i < grid.Length; ++i)
        {
            var row = grid[i];
            for (int j = 0; j < row.Length; ++j)
            {
                if (row[j] == 0)
                    continue;
                
                if(i - 1 < 0 || grid[i-1][j] == 0)
                    ++ans;
                if(i + 1 >= grid.Length || grid[i+1][j] == 0)
                    ++ans;
                if(j - 1 < 0 || grid[i][j-1] == 0)
                    ++ans;
                if(j + 1 >= row.Length || grid[i][j+1] == 0)
                    ++ans;
            }
        }
        return ans;
    }
    
}
// @lc code=end

